package demo1;

import sun.rmi.runtime.Log;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class LoginView extends JFrame {
     JLabel titleLabel = new JLabel("学生成绩管理系统登录",JLabel.CENTER);
     JLabel  userLabel = new JLabel("用户名:");
     JTextField userText = new JTextField();
     JLabel  passwordLabel = new JLabel("密码:");
     JPasswordField passwordText = new JPasswordField();
     JButton   lonButton = new JButton("登录");
      JButton  resetButton = new JButton("重置") ;
      SpringLayout springLayout = new SpringLayout();
      JPanel centerPanel = new JPanel(springLayout);
      LoginHandler loginHandler ;

    public JTextField getUserText() {
        return userText;
    }

    public JPasswordField getPasswordText() {
        return passwordText;
    }

    public LoginView(){
        super("学生成绩管理系统");
        Container contentPane = getContentPane();
        //设置各个组件的字体
        setFont();

        //处理事件
        loginHandler = new LoginHandler(this) ;

         //给两个按钮设置 事件          把loginHandler对象传过去
        lonButton.addActionListener(loginHandler);
         resetButton.addActionListener(loginHandler);



        //添加组件 到内容面板 ；
        addPane(contentPane);

        //弹簧布局 ；
        layoutCenter();

        URL kunUrl = LoginView.class.getClassLoader().getResource("image\\kun.jpg");
        Image image = new ImageIcon(kunUrl).getImage();
        setIconImage(image);
        setVisible(true);
        setSize(600,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void setFont() {
        userText.setPreferredSize(new Dimension(200,30));
        passwordText.setPreferredSize(new Dimension(200,30));
        // 标题字体设置
        Font titleFont = new Font("华文行楷", Font.PLAIN, 40);
        titleLabel.setFont(titleFont);
        titleLabel.setPreferredSize(new Dimension(0, 80));
        //按钮字体设置
        Font conterFont = new Font("楷体", Font.PLAIN, 20);
        userLabel.setFont(conterFont);
        passwordLabel.setFont(conterFont);
        resetButton.setFont(conterFont);
        lonButton.setFont(conterFont);
    }

    private void addPane(Container contentPane) {
        //把组件加入面板;
        centerPanel.add(userLabel);
        centerPanel.add(userText);
        centerPanel.add(passwordLabel);
        centerPanel.add(passwordText);
        centerPanel.add(lonButton);
        centerPanel.add(resetButton);
        //把标题放在内容目标的 北边， centerPanel 在中间 ；
        contentPane.add(titleLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);
    }

    private void layoutCenter() {
        //      弹簧布局中间部分的 ；
//       弹簧布局 设置userLabel的位置 让他水平居中 北边距离centerPanel的北边20；
        Spring sumOffset = Spring.sum(Spring.sum(Spring.width(userLabel),
                Spring.width(userText)), Spring.constant(20));
        int offsetX = sumOffset.getValue()/2 ;
        springLayout.putConstraint(SpringLayout.WEST ,userLabel,-offsetX , SpringLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH ,userLabel,20 , SpringLayout.NORTH,centerPanel);
        //userText
        springLayout.putConstraint(SpringLayout.WEST ,userText,20 , SpringLayout.EAST,userLabel);
        springLayout.putConstraint(SpringLayout.NORTH ,userText,0 , SpringLayout.NORTH,userLabel);
        //passwordLabel 和 passwordText
        springLayout.putConstraint(SpringLayout.NORTH ,passwordLabel,20 , SpringLayout.SOUTH,userLabel);
        springLayout.putConstraint(SpringLayout.EAST ,passwordLabel,0 , SpringLayout.EAST,userLabel);
        springLayout.putConstraint(SpringLayout.WEST ,passwordText,20 , SpringLayout.EAST,passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH ,passwordText,0 , SpringLayout.NORTH,passwordLabel);
        //lonButton  和 resetButton
        springLayout.putConstraint(SpringLayout.WEST ,lonButton,40 , SpringLayout.WEST,userLabel);
        springLayout.putConstraint(SpringLayout.NORTH ,lonButton,20 , SpringLayout.SOUTH,passwordLabel);
        springLayout.putConstraint(SpringLayout.WEST ,resetButton,40 , SpringLayout.EAST,lonButton);
        springLayout.putConstraint(SpringLayout.NORTH ,resetButton,0 , SpringLayout.NORTH,lonButton);
    }

    public static void main(String[] args) {
        new LoginView();
    }

}
